const {
  addDept,
  getDeptById,
  deleteDept,
  updateDept,
  getDepts,
} = require("@/models/admin/dept-model");

/**
 * 添加部门
 */
exports.append = async (req, res, next) => {
  try {
    if (!req.body.name) throw new Error("请输入部门名称");
    const result = await addDept(req.body);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 删除部门
 */
exports.remove = async (req, res, next) => {
  try {
    var result = await getDeptById({ id: req.params.id });
    if (!result) throw new Error("部门不存在");
    result = await deleteDept(req.params.id);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 编辑部门
 */
exports.edit = async (req, res, next) => {
  try {
    if (!req.body.name) throw new Error("请输入部门名称");
    const result = await updateDept(req.params.id, req.body);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 查询部门
 */
exports.page = async (req, res, next) => {
  try {
    const result = await getDepts(req.query);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 部门详情
 */
exports.detail = async (req, res, next) => {
  try {
    const result = await getDeptById({ id: req.params.id });
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};
